package cn.zjj.pet.service.impl;

import cn.zjj.basic.exception.BusinessException;
import cn.zjj.basic.service.impl.BaiduAiAuditServiceImpl;
import cn.zjj.basic.service.impl.BaseServiceImpl;
import cn.zjj.basic.util.CodeGenerateUtils;
import cn.zjj.basic.util.DistanceUtils;
import cn.zjj.basic.util.PageList;
import cn.zjj.basic.util.Point;
import cn.zjj.order.domain.OrderPetAcquisition;
import cn.zjj.order.mapper.OrderPetAcquisitionMapper;
import cn.zjj.org.domain.Employee;
import cn.zjj.org.domain.Shop;
import cn.zjj.org.mapper.EmployeeMapper;
import cn.zjj.org.mapper.ShopMapper;
import cn.zjj.pet.domain.Pet;
import cn.zjj.pet.domain.PetDetail;
import cn.zjj.pet.domain.SearchMasterMsg;
import cn.zjj.pet.domain.SearchMasterMsgAuditLog;
import cn.zjj.pet.dto.AcceptDto;
import cn.zjj.pet.mapper.PetDetailMapper;
import cn.zjj.pet.mapper.PetMapper;
import cn.zjj.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.zjj.pet.mapper.SearchMasterMsgMapper;
import cn.zjj.pet.query.SearchMasterMsgQuery;
import cn.zjj.pet.service.ISearchMasterMsgService;

import cn.zjj.rocketmq.consumer.RocketMQMsgListener;
import cn.zjj.rocketmq.producer.RocketMQProducer;
import cn.zjj.user.domain.User;
import cn.zjj.user.mapper.UserMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;


@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;

    @Autowired
    private BaiduAiAuditServiceImpl baiduAiAuditService;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private RocketMQProducer rocketMQProducer;
    //寻主
    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Long id) {
        //空值校验
        if(searchMasterMsg.getAddress()==null||
        searchMasterMsg.getAge()==null||
        searchMasterMsg.getName()==null||
        searchMasterMsg.getGender()==null||
        searchMasterMsg.getCoatColor()==null||
        searchMasterMsg.getPetType()==null||
        searchMasterMsg.getPrice()==null||
        searchMasterMsg.getTitle()==null
        ){
          throw  new BusinessException("数据为必填项,不能为空");
        }
        //添加寻主信息至数据库
        //把userid赋值进去
        User user=userMapper.selectByLogininfoId(id);
        searchMasterMsg.setUser_id(user.getId());
        //把最近的shop的id赋值进去
        Point point = DistanceUtils.getPoint(searchMasterMsg.getAddress());
        Shop nearShop= DistanceUtils.getNearestShop(point,shopMapper.selectAll());
        searchMasterMsg.setShop_id(nearShop.getId());
        searchMasterMsg.setNote("待审核");

        searchMasterMsgMapper.insert(searchMasterMsg);

        //审核

        //文字审核
        Boolean textBoolean = baiduAiAuditService.textAudit(searchMasterMsg.getAddress() + searchMasterMsg.getTitle() + searchMasterMsg.getName() + searchMasterMsg.getCoatColor());
        //图片审核
//        Boolean imageBoolean = baiduAiAuditService.imageAudit(searchMasterMsg.getResources());


        //声明一个审核对象
        SearchMasterMsgAuditLog aduitLog = new SearchMasterMsgAuditLog();

        if(textBoolean){
            //审核通过
            searchMasterMsg.setNote("待处理");
            searchMasterMsg.setState(1);
            //更新
            searchMasterMsgMapper.update(searchMasterMsg);

            aduitLog.setMsg_id(searchMasterMsg.getId());
            aduitLog.setAudit_time(new Date());
            aduitLog.setState(1);
            aduitLog.setNote("待处理");
            searchMasterMsgAuditLogMapper.insert(aduitLog);
        }else{
            //审核未通过
            searchMasterMsg.setNote("审核未通过");
            searchMasterMsg.setState(-1);
            //更新
            searchMasterMsgMapper.update(searchMasterMsg);

            aduitLog.setMsg_id(searchMasterMsg.getId());
            aduitLog.setAudit_time(new Date());
            aduitLog.setState(-1);
            aduitLog.setNote("审核未通过");
            searchMasterMsgAuditLogMapper.insert(aduitLog);

            throw new BusinessException("信息录入异常,审核未通过");
        }
    }


    /**
     * 待处理: 可以被平台员工和所属店铺的员工看到
     * 状态:1
     * @param query
     * @param id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> tohandleQuery(SearchMasterMsgQuery query, Long id) {
        query.setState(1);
        return selectHandleAndAuditQuery(query,id);
    }
    private PageList<SearchMasterMsg> selectHandleAndAuditQuery(SearchMasterMsgQuery query, Long id){
        Employee emp = employeeMapper.selectByLogininfoId(id);
        //如果emp的shop_id 不为空说明为店铺员工,就赋值给query根据shopid查寻主消息
        if(emp.getShop_id()!=null){
            query.setShop_id(emp.getShop_id());
        }
        return super.selectQuery(query);
    }
    //待审核
    @Override
    public PageList<SearchMasterMsg> toAuditQuery(SearchMasterMsgQuery query, Long id) {
        query.setState(0);
        return selectHandleAndAuditQuery(query,id);
    }

    //拒单
    @Override
    public void reject(Long id) {
        searchMasterMsgMapper.reject(id);
    }
    //接单
    @Override
    public void accept(AcceptDto adto) {
        Long msgId = adto.getMsgId();
        String note = adto.getNote();
        Long handler = adto.getHandler();

        if(StringUtils.isEmpty(handler)){
            throw new BusinessException("必须指派员工!");

        }
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.selectBySearchMasterMsgId(msgId);
        if(orderPetAcquisition!=null){
            throw new BusinessException("订单正在处理,请勿重复提交!");
        }
        //生成pet和petdecail并保存
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.selectById(msgId);
        Pet pet=searchMasterMsg2Pet(searchMasterMsg);
        petMapper.insert(pet);
        PetDetail petDetail=pet2PetDetail(pet);
        petDetailMapper.insert(petDetail);//先保存pet返回id后赋值到宠物详情,再保存petdetail

        //根据三个生成订单
        OrderPetAcquisition opa= innitOrderPetAcquisition(adto,pet,searchMasterMsg);
        //添加订单
        orderPetAcquisitionMapper.insert(opa);
        //获取订单id,ordersn和过期时间
        OrderPetAcquisition order = orderPetAcquisitionMapper.selectById(opa.getId());
        rocketMQProducer.sendDelayMsg(order.getOrderSn(),5);
    }





    //根据SearchMasterMsg生成pet
    Pet searchMasterMsg2Pet(SearchMasterMsg smm){
        Pet pet = new Pet();
        pet.setName(smm.getName());
        pet.setCostprice(smm.getPrice());
        pet.setType_id(smm.getPetType());
        pet.setResources(smm.getResources());
        pet.setShop_id(smm.getShop_id());
        pet.setSearch_master_msg_id(smm.getId());
        return  pet;
    }
    PetDetail pet2PetDetail(Pet pet){
        PetDetail petDetail = new PetDetail();
        petDetail.setPet_id(pet.getId());
        return  petDetail;
    }

    OrderPetAcquisition innitOrderPetAcquisition(AcceptDto adto,Pet pet,SearchMasterMsg searchMasterMsg){
        OrderPetAcquisition opa = new OrderPetAcquisition();
        opa.setOrderSn(CodeGenerateUtils.generateOrderSn(searchMasterMsg.getId()));
        // 摘要
        opa.setDigest("【"+searchMasterMsg.getTitle()+"】");

        //                                                        2d<-24h<-1h<-1m<-1s
        opa.setLastcomfirmtime(new Date(System.currentTimeMillis()+30*1000));  //当前时间加两天,2*24*60*

        opa.setState(0);// 待处理,订单完成才会变,订单的状态
        opa.setPrice(searchMasterMsg.getPrice());
        opa.setPet_id(pet.getId());
        opa.setUser_id(searchMasterMsg.getUser_id());
        opa.setShop_id(searchMasterMsg.getShop_id());
        opa.setAddress(searchMasterMsg.getAddress());
        opa.setEmp_id(adto.getHandler());
        opa.setSearch_master_msg_id(adto.getMsgId());
        return opa;
    }

}
